
const { objMod } = require('../models');

let msgArr = [];
let comsArr = [];
let likesArr = [];
let msgObj;
let comObj;
let likesObj;


let res = {
    "/": ["get", async (ctx) => {

        //获取session的值
        let data = ctx.session.username;

        console.log("session的值：" + data);

        //如果有登陆
        if (data) {
            ctx.redirect('/index');
        } else {
            ctx.render('login.html', {
                title: "欢迎登陆我的微说说"
            });
        }


    }],
    //当注销时
    "/cancelBtn": ["get", async (ctx) => {

        //删除session的值
        ctx.session = null;

        //跳转到登陆界面
        ctx.redirect('/');

    }],
    "/index": ["get", async (ctx) => {

        //获取session的值
        let username = ctx.session.username;

        console.log("session.username的值：" + username);

        //获取messages表的FormUserId,centent
        let msgs = await objMod.Message.findAll({
            attributes: ["id", "fromUserId", "content"],
            order: [
                ["id", "desc"]//根据id倒序
            ],
        });

        console.log("说说数量" + msgs.length);

        if (msgArr.length > 0) {
            msgArr.splice(0, msgArr.length)
        }

        for (let i = 0; i < msgs.length; i++) {

            msgObj = JSON.parse(JSON.stringify(msgs[i]));

            //获取说说id、说说内容
            let msgId = msgObj.fromUserId;
            // let msgContent = msgObj.content;


            // console.log("说说id:" + msgObj.id);
            // console.log("发表说说的用户id：" + msgId);
            // console.log("说说内容：" + msgContent);

            //查询Users表
            let articles = await objMod.Users.findAll({
                attributes: ["id", "username", "password"]
            });
            // 遍历表中的数据
            for (let i = 0; i < articles.length; i++) {
                if (articles[i].id == msgId) {
                    //获取用户名
                    let nameForDatabase = articles[i].username.trim();

                    //将用户名添加到msgObj中
                    msgObj.uName = nameForDatabase;

                }
            }

            msgArr.push(msgObj);

        }

        //获取comments表的 "id", "fromUserId","toUserId", "msgId","content" 
        let coms = await objMod.Comment.findAll({
            attributes: ["id", "fromUserId", "toUserId", "msgId", "content"],
        });

        console.log(coms);

        if (comsArr.length > 0) {
            comsArr.splice(0, comsArr.length)
        }

        //遍历吐槽表
        for (let i = 0; i < coms.length; i++) {
            comObj = JSON.parse(JSON.stringify(coms[i]));


            //获取comments表的"id", "fromUserId","toUserId", "msgId","content"

            //获取吐槽说说的用户id、被回复用户id、说说内容
            let fromUserId = comObj.fromUserId;
            let toUserId = comObj.toUserId;
            let content = comObj.content;

            //说说id
            // let comId = comObj.msgId;

            //点赞的状态
            // let applaud = comObj.applaud;


            // console.log("说说id:" + comId);
            // console.log("当前用户id:" + fromUserId);
            // console.log("被回复id:" + toUserId);
            // console.log("吐槽内容:" + content);
            // console.log("点赞的状态:" + applaud);



            //查询Users表
            let articles = await objMod.Users.findAll({
                attributes: ["id", "username", "password"]
            });

            // 遍历表中的数据
            for (let i = 0; i < articles.length; i++) {
                if (articles[i].id == fromUserId) {
                    //获取评论用户名
                    let nameForDatabase = articles[i].username.trim();

                    //将用户名添加到msgObj中
                    comObj.fromUserName = nameForDatabase;

                } else if (articles[i].id == toUserId) {
                    //  获取被回复用户名 将用户名添加到msgObj中
                    comObj.toUserName = articles[i].username.trim();
                }
            }


            comsArr.push(comObj);

        }


        //获取like表的 "id",  userId | msgId | applaud
        let likes = await objMod.Like.findAll({
            attributes: ["id", "userId", "msgId", "applaud"],
        });

        console.log(likes);

        if (likesArr.length > 0) {
            likesArr.splice(0, likesArr.length)
        }

        console.log("likes的数量" + likes.length);

        //遍历点赞表
        for (let i = 0; i < likes.length; i++) {
            likesObj = JSON.parse(JSON.stringify(likes[i]));

            //获取like表的 "id",  userId | msgId | applaud

            //获取点赞的用户id、说说id、点赞状态
            let UserId = likesObj.userId;
            let MsgId = likesObj.msgId;
            let Applaud = likesObj.applaud;


            console.log("用户id:" + UserId);
            console.log("说说id:" + MsgId);
            console.log("点赞的状态:" + Applaud);


            //查询Users表
            let articles = await objMod.Users.findAll({
                where: {
                    id: UserId
                }
            });

            console.log(articles[0]);

            let nameForDatabase = articles[0].username;

            console.log(nameForDatabase);
            likesObj.UserName = nameForDatabase;

            likesArr.push(likesObj);
        }

      console.log("likesArr："+likesArr);

        //如果登陆成功
        if (username) {
            ctx.render('index.html', {
                name: username,
                msgArr: msgArr,
                comsArr: comsArr,
                likesArr: likesArr
            });

        } else {
            ctx.redirect("/");
        }

    }],
}


module.exports = res;